package com.ymatou.iapi.godmch.testcase;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.detailByCsIdBean;
import com.ymatou.iapi.godmch.parameter.mchCreateCsBean;
import com.ymatou.iapi.godmch.service.detailByCsIdCall;
import com.ymatou.iapi.godmch.service.mchCreateCsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.cs_info;
import com.ymttest.database.model.godmch.buyer_invite_code;
import com.ymttest.database.model.godmch.buyer_invite_codeExample;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.buyer_invite_codeWapper;
/**
*客服id查询客服详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_detailByCsId{
	private static detailByCsIdBean bean;
	private static detailByCsIdCall call;
	
	private static Integer mchId_inviteCode=501031336;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服id查询客服详情");
	}

	@Before
	public void caseUp() {
		bean=new detailByCsIdBean();
		call=new detailByCsIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void check_verify(mchCreateCsBean mchCreateCsBean,boolean isCode) throws JSONException, InterruptedException
	{
		cs_info cs=new DefGodMchWapper().selectCsInfo(mchCreateCsBean.getMchId(),bean.getCsId()).get(0);
		JSONObject obj=call.getResult();
		Logger.verifyEquals("200", call.getString("status"), "验证status");
		Logger.verifyEquals(obj.getInt("templateId"), cs.getTempldateId(), "验证TemplateId");
		Logger.verifyEquals(obj.getString("title"), cs.getTitle(), "验证Title");
		if(isCode==true)
		{
			buyer_invite_codeExample example=new buyer_invite_codeExample();
			example.createCriteria().andUserIdEqualTo(bean.getCsId());
			buyer_invite_codeWapper codeMapper=new buyer_invite_codeWapper();
			buyer_invite_code code=codeMapper.selectByExample(example).get(0);	
			Logger.verifyEquals(obj.getString("inviteCode"), code.getInviteCode(), "验证inviteCode");
		}
	   Logger.verifyEquals(obj.getString("remark"), cs.getRemark(), "验证Remark");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailByCsId_001() {
		Logger.start(true,"客服id查询客服详情-普通客服-返回成功");
		try {

			mchCreateCsBean mchCreateCsBean=GodmchManageCallService.initMchCreateCsBean();
			GodmchManageCallService.mchCreateCsCall(mchCreateCsBean);
			cs_info csi=new DefGodMchWapper().selectCsInfoByMchId(mchCreateCsBean.getMchId()).get(0);
			
			bean.setCsId(csi.getCsId());
			call.setData(bean);
			call.callService();			
			
			check_verify(mchCreateCsBean,false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailByCsId_002() {
		Logger.start(true,"客服id查询客服详情-邀请制客服-返回成功");
		try {

			mchCreateCsBean mchCreateCsBean=GodmchManageCallService.initMchCreateCsBean();
			GodmchManageCallService.mchCreateCsCall(mchCreateCsBean);
			cs_info csi=new DefGodMchWapper().selectCsInfoByMchId(mchCreateCsBean.getMchId()).get(0);
			//设置商户是邀请制
		    GodMemberService.setCollectionMobile(mchCreateCsBean.getMchId(), true);
            GodMemberService.setInvitationCodeCall(mchCreateCsBean.getMchId(), true, 0);
			
			bean.setCsId(csi.getCsId());
			call.setData(bean);
			call.callService();				
			
			check_verify(mchCreateCsBean,true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailByCsId_003() {
		Logger.start(true,"客服id查询客服详情-csId不存在-返回失败");
		try {
			
			bean.setCsId(99999999);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailByCsId_004() {
		Logger.start(true,"客服id查询客服详情-csId为空-返回失败");
		try {
			//bean.setCsId(99999999);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getString("status"), "验证status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}